package com.recharge.web.controller;


import com.recharge.common.domain.PageDO;
import com.recharge.common.enums.ErrorCode;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.ResultSupport;
import com.recharge.common.result.web.ObjectResponse;
import com.recharge.common.util.Constant;
import com.recharge.domain.query.ProviderApiQueryDO;
import com.recharge.domain.vo.ProviderApiDO;
import com.recharge.domain.vo.UserLoginDO;
import com.recharge.service.ProviderApiService;
import com.recharge.service.httpInterFace.ProviderInterFace;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 上游供应商渠道API 前端控制器
 * </p>
 *
 * @author reagan
 * @since 2020-08-19
 */
@RestController
@RequestMapping("/provider-api")
public class ProviderApiController extends BaseController {

    @Autowired
    private ProviderApiService providerApiService;

    @RequiresPermissions("provider-api:add")
    @RequestMapping(value = "/add.html", method = RequestMethod.POST)
    @ApiOperation(value = "增加供货商API信息", notes = "")
    public @ResponseBody
    ObjectResponse<ProviderApiDO> add(@RequestBody ProviderApiDO domain) {

        ObjectResponse<ProviderApiDO> response = new ObjectResponse<>();

        ResultDO result = providerApiService.add(domain);

        if (!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("provider-api:edit")
    @RequestMapping(value = "/edit.html", method = RequestMethod.POST)
    @ApiOperation(value = "编辑供货商API信息", notes = "")
    public @ResponseBody
    ObjectResponse<ProviderApiDO> edit(@RequestBody ProviderApiDO domain) {
        ObjectResponse<ProviderApiDO> response = new ObjectResponse<>();

        ResultDO result = new ResultSupport();
        try {
            result = providerApiService.edit(domain);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("provider-api:get")
    @RequestMapping(value = "/get.html", method = RequestMethod.POST)
    @ApiOperation(value = "获取供货商API信息根据供应商ID", notes = "")
    public @ResponseBody
    ObjectResponse<ProviderApiDO> getByProviderId(@RequestBody ProviderApiDO domain) {

        ObjectResponse<ProviderApiDO> response = new ObjectResponse<>();
        String providerNo = domain.getProviderNo();
        if (providerNo == null) {
            response.setStatus(ErrorCode.PARAM_GET_ERROR.getCode());
            response.setMsg(ErrorCode.PARAM_GET_ERROR.getMsg());
            return response;
        }

        ResultDO result = providerApiService.getByProviderNo(providerNo);

        if (!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((ProviderApiDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    @RequiresPermissions("provider-api:set")
    @RequestMapping(value = "/set.html", method = RequestMethod.POST)
    @ApiOperation(value = "编辑供货商API信息", notes = "")
    public @ResponseBody
    ObjectResponse<ProviderApiDO> set(@RequestBody ProviderApiDO domain) {
        domain.setIsSync(Constant.IS_DATA_NO_SYNC);
        ObjectResponse<ProviderApiDO> response = new ObjectResponse<>();

        if (!domain.verification()) {
            response.setStatus(ErrorCode.PARAM_TEMPLATE_ERROR.getCode());
            response.setMsg(ErrorCode.PARAM_TEMPLATE_ERROR.getMsg());
            return response;
        }
        domain.trim();

        ResultDO result = new ResultSupport();
        try {
            if (domain.getId() == null) {
                result = providerApiService.add(domain);
            } else {
                result = providerApiService.edit(domain);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }


    @RequiresPermissions("provider-api:ProviderApi")
    @RequestMapping(value = "/ProviderApi.html", method = RequestMethod.POST)
    @ApiOperation(value = "获取供货商API信息", notes = "")
    public @ResponseBody
    ObjectResponse<ProviderApiDO> get(@RequestBody ProviderApiDO domain) {

        ObjectResponse<ProviderApiDO> response = new ObjectResponse<>();

        ResultDO result = providerApiService.get(domain.getId());

        if (!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((ProviderApiDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    @RequiresPermissions("provider-api:delete")
    @RequestMapping(value = "/delete.html", method = RequestMethod.POST)
    @ApiOperation(value = "删除供货商API信息", notes = "")
    public @ResponseBody
    ObjectResponse delete(@RequestBody ProviderApiDO domain) {

        ObjectResponse response = new ObjectResponse();
        UserLoginDO userLoginDO = getUserLoginInfo();

        ResultDO result = providerApiService.remove(domain.getId(), userLoginDO.getUsername());

        if (!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("provider-api:page")
    @RequestMapping(value = "/page.html", method = RequestMethod.POST)
    @ApiOperation(value = "分页查询供货商API", notes = "")
    public @ResponseBody
    ObjectResponse<PageDO> page(@RequestBody ProviderApiQueryDO query) {

        ObjectResponse<PageDO> response = new ObjectResponse<>();

        ResultDO result = providerApiService.find(query);

        if (result.isSuccess()) {
            response.setData((PageDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }

        return response;
    }

    @RequiresPermissions("provider-api:interfaces")
    @RequestMapping(value = "/interfaces.html", method = RequestMethod.POST)
    @ApiOperation(value = "查询已接入系统接口信息", notes = "")
    public @ResponseBody
    ObjectResponse<List<Map<String,Object>>> getInterface() {

        ObjectResponse<List<Map<String,Object>>> response = new ObjectResponse<>();
        ProviderInterFace[] values = ProviderInterFace.values();

        List<Map<String,Object>> list = new ArrayList<>();
        for (ProviderInterFace value : values) {
            Map<String,Object> map = new HashMap<>();
            map.put("num", value.getNumber());
            map.put("name", value.getName());
            list.add(map);
        }
        response.setData(list);
        response.setStatus(ErrorCode.SYSTEM_SUCCESS.getCode());
        response.setMsg(ErrorCode.SYSTEM_SUCCESS.getMsg());
        return response;
    }

}
